package com.family.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.family.common.result.Result;
import com.family.model.system.SysUser;
import com.family.model.vo.AssignRoleVo;
import com.family.model.vo.SysUserQueryVo;
import com.family.system.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

/**
 * @ClassName SysUserController
 * @Date 2023-05-08 14:21
 * @AUTHOR family
 **/
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/system/sysUser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("分页查询")
    @GetMapping("{page}/{limit}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result<Object> getPageList(
            @PathVariable Long page,
            @PathVariable Long limit,
            SysUserQueryVo sysUserQueryVo) {
        Object userList = redisTemplate.opsForValue().get("userList" + page);
        if (!StringUtils.isEmpty(userList)) {
            return Result.ok(userList);
        }
        IPage<SysUser> userPage = sysUserService.selectPage(new Page<>(page, limit), sysUserQueryVo);
        return Result.ok(userPage);
    }


    @ApiOperation("添加用户")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('bnt.sysUser.add')")
    public Result<Object> addUser(@RequestBody SysUser sysUser) {
        boolean row = sysUserService.save(sysUser);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("userList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }

    @ApiOperation("根据id查询用户")
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result<Object> findById(@PathVariable Long id) {
        SysUser sysUser = sysUserService.getById(id);
        return Result.ok(sysUser);
    }

    @ApiOperation("更新用户信息")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    public Result<Object> updateUser(@RequestBody SysUser sysUser) {
        boolean row = sysUserService.updateById(sysUser);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("userList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }

    @ApiOperation("通过Id删除用户")
    @DeleteMapping("remove/{id}")
    @PreAuthorize("hasAuthority('bnt.sysUser.remove')")
    public Result<Object> removeById(@PathVariable Long id) {
        boolean row = sysUserService.removeById(id);
        if (!row) {
            return Result.fail();
        }
        Set<Object> keys = redisTemplate.keys("userList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }

    @ApiOperation("更新用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    @PreAuthorize("hasAuthority('bnt.sysUser.update')")
    public Result<Object> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        Result<Object> result = sysUserService.updateStatus(id, status);
        Set<Object> keys = redisTemplate.keys("userList" + "*");
        redisTemplate.delete(keys);
        return result;
    }

    @ApiOperation("根据Id查询用户的角色和所有角色")
    @GetMapping("toAssign/{userId}")
    @PreAuthorize("hasAuthority('bnt.sysUser.list')")
    public Result<Object> getRolesByUserId(@PathVariable Long userId) {
        Map<String, Object> map = sysUserService.getRolesByUserId(userId);
        return Result.ok(map);
    }

    @ApiOperation("分配角色")
    @PostMapping("doAssign")
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    public Result<Object> doAssign(@RequestBody AssignRoleVo assignRoleVo) {
        sysUserService.doAssign(assignRoleVo);
        Set<Object> keys = redisTemplate.keys("userList" + "*");
        redisTemplate.delete(keys);
        return Result.ok();
    }
}
